package logic

import (
	"context"
	"gorm.io/gorm"
	"meituan-waimai/models/mysql"

	"meituan-waimai/meituanrpc/internal/svc"
	"meituan-waimai/meituanrpc/meituanrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CouponGrantLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCouponGrantLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CouponGrantLogic {
	return &CouponGrantLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CouponGrantLogic) CouponGrant(in *meituanrpc.CouponGrantRequest) (*meituanrpc.CouponGrantResponse, error) {
	// todo: add your logic here and delete this line
	// 优惠券发放
	coupon := mysql.Coupon{
		Model:           gorm.Model{},
		CouponName:      in.CouponName,
		CouponThreshold: float64(in.CouponThreshold),
		CouponAmount:    float64(in.CouponAmount),
		CouponDiscount:  int(in.CouponDiscount),
		CouponType:      in.CouponType,
	}
	mysql.DB.Create(&coupon)
	return &meituanrpc.CouponGrantResponse{
		CouponId: int64(coupon.ID),
	}, nil
}
